home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / loTeX / loTeX.app / Controller.m < prev    next >
Encoding:
Text File  |  1992-07-28  |  4.7 KB  |  200 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Controller.h"
  5. #import "SwapView.h"
  6. #import <appkit/Application.h>
  7. #import <appkit/ButtonCell.h>
  8. #import <appkit/Matrix.h>
  9. #import <appkit/Panel.h>
  10. #import <appkit/ScrollView.h>
  11. #import <appkit/Text.h>
  12. #import <objc/HashTable.h>
  13. #import "string.h"
  14. #import "stdio.h"
  15. #import "stdlib.h"
  16.  
  17. @implementation Controller
  18.  
  19. /* get the text of the selection as a string */
  20. const char *copySel(id target)
  21. {
  22.     NXSelPt start, end;
  23.     char *buf;
  24.     [target getSel:&start :&end];
  25.     if (start.cp > end.cp) { start.cp= end.cp= 0; }
  26.     buf= (char *) malloc(end.cp - start.cp +1);
  27.     [target getSubstring:buf start:start.cp length:(end.cp - start.cp)];
  28.     buf[end.cp - start.cp] = '\0';
  29.     return (const char *) buf;
  30. }
  31.  
  32. /* notify the delegate of the text object that the text changed */
  33. void notify(id target)
  34. {
  35.     id delegate= [target delegate];
  36.     if (delegate && [delegate respondsTo:@selector(textDidGetKeys:isEmpty:)]) {
  37.     [delegate 
  38.         perform:@selector(textDidGetKeys:isEmpty:)
  39.         with:target with:(id)NO]; /* ugh,ugh */
  40.     }
  41. }
  42.  
  43. - useLabelAsDecl:sender
  44. {
  45.     const char *n= [[sender selectedCell] title];
  46.     const char *sel= copySel([text_view docView]);
  47.     char *buf;
  48.     if (!n) fprintf(stderr,"no label\n");
  49.     else {
  50.         buf= malloc(strlen(sel)+ strlen(n) + 6);
  51.         sprintf(buf,"{\\%s %s}", n, sel);
  52.     notify([[text_view docView] replaceSel:buf]);
  53.     free(buf); free((char*)sel);
  54.     }    
  55.     return self;
  56. }
  57.  
  58. - useIconNameAsDecl:sender
  59. {
  60.     const char *n= [[sender selectedCell] icon];
  61.     const char *sel= copySel([text_view docView]);
  62.     char *buf;
  63.     if (!n) fprintf(stderr,"no icon name\n");
  64.     else {
  65.         buf= malloc(strlen(sel)+ strlen(n) + 6);
  66.         sprintf(buf,"{%s %s}", n, sel);
  67.     notify([[text_view docView] replaceSel:buf]);
  68.     free(buf); free((char*)sel);
  69.     }    
  70.     return self;
  71. }
  72. - useIconNameAsPrintf:sender
  73. {
  74.     const char *n= [[sender selectedCell] icon];
  75.     const char *sel= copySel([text_view docView]);
  76.     char *buf;
  77.     if (!n) fprintf(stderr,"no icon name\n");
  78.     else {
  79.         buf= malloc(strlen(sel)+ strlen(n) + 6);
  80.         sprintf(buf, n, sel);
  81.     notify([[text_view docView] replaceSel:buf]);
  82.     free(buf); free((char*)sel);
  83.     }    
  84.     return self;
  85. }
  86. - useLabelAsCmd:sender
  87. {
  88.     const char *n= [[sender selectedCell] title];
  89.     const char *sel= copySel([text_view docView]);
  90.     char *buf;
  91.     if (!n) fprintf(stderr,"no label\n");
  92.     else {
  93.         buf= malloc(strlen(sel)+ strlen(n) + 6);
  94.         sprintf(buf,"\\%s{%s}", n, sel);
  95.     notify([[text_view docView] replaceSel:buf]);
  96.     free(buf); free((char*)sel);
  97.     }    
  98.     return self;
  99. }
  100.  
  101. - useIconNameAsCmd:sender
  102. {
  103.     const char *n= [[sender selectedCell] icon];
  104.     const char *sel= copySel([text_view docView]);
  105.     char *buf;
  106.     if (!n) fprintf(stderr,"no icon name\n");
  107.     else {
  108.         buf= malloc(strlen(sel)+ strlen(n) + 6);
  109.         sprintf(buf,"%s{%s}", n, sel);
  110.     notify([[text_view docView] replaceSel:buf]);
  111.     free(buf); free((char*)sel);
  112.     }    
  113.     return self;
  114. }
  115.  
  116. - insertLabel:sender
  117. {
  118.     const char *n= [[sender selectedCell] title];
  119.     char *buf;
  120.     if (!n) fprintf(stderr,"no label\n");
  121.     else {
  122.         buf= malloc(strlen(n) + 6);
  123.     sprintf(buf,"\\%s ", n);
  124.     notify([[text_view docView] replaceSel:buf]);
  125.     free(buf);
  126.     }
  127.     return self;
  128. }
  129.  
  130. - insertIconName:sender
  131. {
  132.     const char *n= [[sender selectedCell] icon];
  133.     char *buf;
  134.     if (!n) fprintf(stderr,"no icon name\n");
  135.     else {
  136.         buf= malloc(strlen(n) + 6);
  137.     sprintf(buf,"%s ", n);
  138.     notify([[text_view docView] replaceSel:buf]);
  139.     free(buf);
  140.     }
  141.     return self;
  142. }
  143.  
  144. - setup
  145. {
  146.     [[swap_place window] setBecomeKeyOnlyIfNeeded:YES];
  147.     panel_table= [[HashTable alloc] initKeyDesc:"*" valueDesc:"@"]; 
  148.     return self;
  149. }
  150.  
  151. - setup:sender
  152. {
  153.     return [self setup];
  154. }
  155.  
  156. - appDidInit:sender
  157. {
  158.     return [self setup:sender];
  159. }
  160.  
  161. /* swap in new panel */
  162. - switchToIconName:sender
  163. {
  164.     panel_name= [[sender selectedCell] icon];
  165.     if (!panel_name) {
  166.         fprintf(stderr,"switchToIconName: no icon name\n");
  167.     } else {
  168.         [swap_place perform:@selector(swapIt:) with:self 
  169.             afterDelay:10 cancelPrevious:NO];
  170.     }
  171.     return self;
  172. }
  173.  
  174. /* swapView delegate method */
  175. - whatPanel
  176. {
  177.     id target_panel= (id) [panel_table valueForKey:panel_name];
  178.     char *buf;
  179.  
  180.     if (target_panel) return target_panel;
  181.     else {
  182.         buf= (char*) malloc(strlen(panel_name) + 6);
  183.     sprintf(buf,"%s.nib", panel_name);
  184.     [NXApp loadNibSection:(const char *)buf owner:self];
  185.     free(buf);
  186.     if (!returned_panel) {
  187.         NXRunAlertPanel("loTeX", "Can't load requested Nib section",
  188.                     0, 0, 0);
  189.         return nil;
  190.     }
  191.     [panel_table insertKey:(const void *) panel_name 
  192.                  value: (void *) returned_panel];
  193.     target_panel= returned_panel;
  194.     returned_panel= nil;
  195.     return target_panel;
  196.     }
  197. }
  198.  
  199. @end
  200.